/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package parser.serverData.item;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;

import parser.clientData.clientItems.ClientItem;
import parser.clientData.clientSkills.SkillBaseClient;
import parser.clientData.clientStrings.ClientString;
import parser.util.DataManager;

/**
 * @author Mr. Poke
 *
 */
public class ItemSave
{
	public static void save()
	{
		ObjectFactory objFactory = new ObjectFactory();
		ItemTemplates collection = objFactory.createItemTemplates();
		List<ItemTemplate> templateList = collection.getItemTemplate();

		for (ClientItem clientItem : DataManager.getInstance().getItemList())
		{
			ItemTemplate item = new ItemTemplate();
			item.setId(clientItem.getId());
			if (clientItem.getDesc() != null)
			{
				ClientString string = DataManager.getInstance().getStringNameMap().get(clientItem.getDesc().toLowerCase());
				if (string != null)
				{
					item.setDesc(string.getId() * 2 + 1);
				}
				else
				{
					System.out.println("Missing item desc: " + clientItem.getDesc());
				}
			}
	
			if (clientItem.getUseDelay() != 0)
				item.setUsedelay(clientItem.getUseDelay());
			if (clientItem.getUseDelayTypeId() != 0)
				item.setUsedelayid(clientItem.getUseDelayTypeId());

			if (clientItem.getEquipmentSlots() != null && !clientItem.getEquipmentSlots().equalsIgnoreCase("none"))
				item.setSlot(getItemSlot(clientItem.getEquipmentSlots()));
			if (clientItem.getWeaponType() != null)
				item.setEquipmentType(EquipType.WEAPON);
			else if (clientItem.getArmorType() != null)
				item.setEquipmentType(EquipType.ARMOR);
			else if (clientItem.getGainSkill1() != null)
				item.setEquipmentType(EquipType.STIGMA);
			else
				item.setEquipmentType(EquipType.NONE);

			if (clientItem.getAttackType() != null)
			{
				item.setAttackType(EAttackType.fromValue(clientItem.getAttackType().toUpperCase()));
			}

			if (item.getSlot() != null)
			{
				switch (item.getSlot())
				{
					case (1 << 10):
					case 192:
					case 768:
						item.setEquipmentType(EquipType.ARMOR);
				}
			}

			item.setLevel(clientItem.getLevel());
			item.setMask(getItemMask(clientItem));
			if (clientItem.getArmorType() != null)
			{
				item.setArmorType(getArmorType(clientItem.getArmorType()));
			}

			if (item.getId() >= 115000000 && item.getId() < 116000000)
			{
				item.setArmorType(ArmorType.SHIELD);
				item.setItemCategory("SHIELD");
			}
			else if (item.getId() >= 123000000 && item.getId() < 125000000)
			{
				item.setItemCategory("BELT");
				item.setEquipmentType(EquipType.ARMOR);
			}
			else if (item.getId() >= 163000000 && item.getId() < 163050000)
			{
				item.setArmorType(ArmorType.ARROW);
				item.setItemCategory("ARROW");
				item.setEquipmentType(EquipType.ARMOR);
			}
			else if (item.getId() >= 187000000 && item.getId() < 188000000)
			{
				item.setItemCategory("FEATHER");
			}

			if (clientItem.getWeaponType() != null)
				item.setWeaponType(getWeaponType(clientItem.getWeaponType()));
			if (clientItem.getItemType() != null)
				item.setItemType(clientItem.getItemType().toUpperCase());
			else
				item.setItemType("NORMAL");

			if (clientItem.getWeaponBoostValue() != 0)
			{
				item.setWeaponBoost(clientItem.getWeaponBoostValue());
				item.setArmorType(ArmorType.SHARD);
				item.setEquipmentType(EquipType.ARMOR);
				item.setSlot(24576);
			}

			if (clientItem.getProcEnchantSkill() != null)
			{
				Godstone godstone = new Godstone();
				SkillBaseClient skill = DataManager.getInstance().getClientSkillsnameMap().get(clientItem.getProcEnchantSkill().toUpperCase());
				if (skill != null)
				{
					int skillId = DataManager.getInstance().getClientSkillsnameMap().get(clientItem.getProcEnchantSkill().toUpperCase()).getId();
					godstone.setSkillid(skillId);
					godstone.setSkilllvl(clientItem.getProcEnchantSkillLevel());
					if (clientItem.getProcEnchantEffectOccurLeftProb() != 0)
						godstone.setProbabilityleft(clientItem.getProcEnchantEffectOccurLeftProb());
					godstone.setProbability(clientItem.getProcEnchantEffectOccurProb());
					item.setGodstone(godstone);
				}
				else
					System.out.println("Missing skill: " + clientItem.getProcEnchantSkill());
			}

			if (clientItem.getReturnWorldid() != 0)
				item.setReturnWorld(clientItem.getReturnWorldid());
			if (clientItem.getReturnAlias() != null)
				item.setReturnAlias(clientItem.getReturnAlias().toUpperCase());

			if (clientItem.getGainSkill1() != null)
			{
				Stigma stigma = new Stigma();
				stigma.setShard(clientItem.getRequireShard());
				stigma.setSkilllvl(clientItem.getGainLevel1());
				int skillId = DataManager.getInstance().getClientSkillsnameMap().get(clientItem.getGainSkill1().toUpperCase()).getId();
				stigma.setSkillid(skillId);
				item.setStigma(stigma);
				if (clientItem.getRequireSkill1() != null)
				{
					StringTokenizer st = new StringTokenizer(clientItem.getRequireSkill1(), ",");
					RequireSkill requireSkill = new RequireSkill();
					requireSkill.setSkilllvl(clientItem.getRequireSkill1Lv());
					while (st.hasMoreElements())
					{
						skillId = DataManager.getInstance().getClientSkillsnameMap().get(st.nextToken().trim().toUpperCase()).getId();
						requireSkill.getSkillId().add(skillId);
					}
					stigma.getRequireSkill().add(requireSkill);
				}
				if (clientItem.getRequireSkill2() != null)
				{
					StringTokenizer st = new StringTokenizer(clientItem.getRequireSkill2(), ",");
					RequireSkill requireSkill = new RequireSkill();
					requireSkill.setSkilllvl(clientItem.getRequireSkill2Lv());
					while (st.hasMoreElements())
					{
						skillId = DataManager.getInstance().getClientSkillsnameMap().get(st.nextToken().trim().toUpperCase()).getId();
						requireSkill.getSkillId().add(skillId);
					}
					stigma.getRequireSkill().add(requireSkill);
				}
			}
			item.setMaxStackCount(clientItem.getMaxStackCount());
			item.setQuality(ItemQuality.valueOf(clientItem.getQuality().toUpperCase()));
			item.setPrice(clientItem.getPrice());
			item.getRestrict().addAll(getRestrict(clientItem));

			if (clientItem.getRacePermitted() != null)
			{
				if (clientItem.getRacePermitted().equalsIgnoreCase("pc_light"))
					item.setRace(ItemRace.ELYOS);
				else if (clientItem.getRacePermitted().equalsIgnoreCase("pc_dark"))
					item.setRace(ItemRace.ASMODIANS);
			}

			Modifiers modifiers = new Modifiers();
			
			WeaponStats weaponStats = new WeaponStats();
			if (clientItem.getMinDamage() != 0 && clientItem.getMaxDamage() != 0)
			{
				weaponStats.setMinDamage(clientItem.getMinDamage());
				weaponStats.setMaxDamage(clientItem.getMaxDamage());
			}

			if (clientItem.getAttackDelay() != 0)
			{
				weaponStats.setAttackSpeed(clientItem.getAttackDelay());
			}

			if (clientItem.getCritical() != 0)
			{
				weaponStats.setPhysicalCritical(clientItem.getCritical());
			}

			if (clientItem.getHitAccuracy() != 0)
			{
				weaponStats.setPhysicalAccuracy(clientItem.getHitAccuracy());
			}

			if (clientItem.getParry() != 0)
			{
				weaponStats.setParry(clientItem.getParry());
			}

			if (clientItem.getMagicalHitAccuracy() != 0)
			{
				weaponStats.setMagicalAccuracy(clientItem.getMagicalHitAccuracy());
			}

			if (clientItem.getMagicalSkillBoost() != 0)
			{
				weaponStats.setBoostMagicalSkill(clientItem.getMagicalSkillBoost());
			}

			if (clientItem.getAttackRange() != 0)
			{
				weaponStats.setAttackRange(Math.round(clientItem.getAttackRange() * 1000));
			}

			if (clientItem.getHitCount() != 0)
			{
				weaponStats.setHitCount(clientItem.getHitCount());
			}

			if (clientItem.getBlock() != 0)
			{
				AddModifier modifier = new AddModifier();
				modifier.setName(Modifiersenum.BLOCK);
				modifier.setValue(clientItem.getBlock());
				modifiers.getAddAndRateAndSet().add(modifier);
			}

			if (clientItem.getDamageReduce() != 0)
			{
				AddModifier modifier = new AddModifier();
				modifier.setName(Modifiersenum.DAMAGE_REDUCE);
				modifier.setValue(clientItem.getDamageReduce());
				modifiers.getAddAndRateAndSet().add(modifier);
			}

			if (clientItem.getMagicalResist() != 0)
			{
				AddModifier modifier = new AddModifier();
				modifier.setName(Modifiersenum.MAGICAL_RESIST);
				modifier.setValue(clientItem.getMagicalResist());
				modifiers.getAddAndRateAndSet().add(modifier);
			}

			if (clientItem.getPhysicalDefend() != 0)
			{
				AddModifier modifier = new AddModifier();
				modifier.setName(Modifiersenum.PHYSICAL_DEFENSE);
				modifier.setValue(clientItem.getPhysicalDefend());
				modifiers.getAddAndRateAndSet().add(modifier);
			}
			List<SimpleModifier> bonuse = getBonusModifier(clientItem);
			
			if (clientItem.getStatEnchantType() != null)
			{
				AddModifier modifier = new AddModifier();
				modifier.setName(getModifiersEnum(clientItem.getStatEnchantType()));
				modifier.setValue(clientItem.getStatEnchantValue());
				modifier.setBonus(true);
				modifiers.getAddAndRateAndSet().add(modifier);
			}
			
			if (weaponStats.getMaxDamage() != null)
				item.setWeaponStats(weaponStats);
			if (!bonuse.isEmpty())
				modifiers.getAddAndRateAndSet().addAll(bonuse);
			if (!modifiers.getAddAndRateAndSet().isEmpty())
				item.getModifiers().add(modifiers);
			ItemActions itemAction = new ItemActions();
			if (clientItem.getSkillToLearn() != null)
			{
				SkillLearnAction action = new SkillLearnAction();
				int skillId = DataManager.getInstance().getClientSkillsnameMap().get(clientItem.getSkillToLearn().toUpperCase()).getId();
				action.setSkillid(skillId);
				itemAction.getSkilllearnAndSkilluseAndEnchant().add(action);
			}
			if(clientItem.getCraftRecipeInfo() != null)
			{
				CraftLearnAction action = new CraftLearnAction();
				action.setRecipeid(DataManager.getInstance().getRecipeNameMap().get(clientItem.getCraftRecipeInfo().toLowerCase()).getId());
				itemAction.getSkilllearnAndSkilluseAndEnchant().add(action);
			}
			if (clientItem.getActivationSkill() != null)
			{
				SkillUseAction skillAction = new SkillUseAction();
				skillAction.setLevel(clientItem.getActivationLevel());
				skillAction.setSkillid(DataManager.getInstance().getClientSkillsnameMap().get(clientItem.getActivationSkill().toUpperCase()).getId());
				itemAction.getSkilllearnAndSkilluseAndEnchant().add(skillAction);
			}
			if (clientItem.getId() == 165000001)
				itemAction.getSkilllearnAndSkilluseAndEnchant().add(new ExtractAction());
			else if (clientItem.getMotionName() != null && clientItem.getMotionName().equalsIgnoreCase("Enchant") && item.getGodstone() == null)
			{
				EnchantItemAction enchantAction = new EnchantItemAction();
				if (clientItem.getSubEnchantMaterialMany() != 0)
					enchantAction.setMaterialCount(clientItem.getSubEnchantMaterialMany());
				itemAction.getSkilllearnAndSkilluseAndEnchant().add(enchantAction);
			}
			
			if (clientItem.getId() == 169100000)
			{
				DyeAction dye = new DyeAction();
				dye.setColor("no");
				itemAction.getSkilllearnAndSkilluseAndEnchant().add(dye);
			}
			else if (clientItem.getDyeingColor() != null)
			{
				DyeAction dye = new DyeAction();
				 String[] colors = clientItem.getDyeingColor().split(",");
				 String color = "";
				 for (String string : colors)
				 {
					 color += Integer.toHexString(Integer.valueOf(string));
				 }
				 dye.setColor(color);
				 itemAction.getSkilllearnAndSkilluseAndEnchant().add(dye);
			}
			if (!itemAction.getSkilllearnAndSkilluseAndEnchant().isEmpty())
				item.getActions().add(itemAction);

			if (clientItem.getOptionSlotBonus() != 0)
				item.setOptionSlotBonus(clientItem.getOptionSlotBonus());
			
			templateList.add(item);
			
		}
		try
		{
			JAXBContext jaxbContext = JAXBContext.newInstance("parser.serverData.item");
			Marshaller marshaller = jaxbContext.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
			marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "item_templates.xsd");
			marshaller.marshal(collection, new FileOutputStream("static_data/item/item_templates.xml"));
		}
		catch (PropertyException e)
		{
			e.printStackTrace();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (JAXBException e)
		{
			e.printStackTrace();
		}
	}

	private static int getItemMask(ClientItem item)
	{
		int mask = 0;
		if (item.getLore() != null && item.getLore().equalsIgnoreCase("TRUE"))
			mask += 1;
		if (item.getCanExchange() != null && item.getCanExchange().equalsIgnoreCase("TRUE"))
			mask += (1 << 1);
		if (item.getCanSellToNpc() != null && item.getCanSellToNpc().equalsIgnoreCase("TRUE"))
			mask += (1 << 2);
		if (item.getCanDepositToCharacterWarehouse() != null && item.getCanDepositToCharacterWarehouse().equalsIgnoreCase("TRUE"))
			mask += (1 << 3);
		if (item.getCanDepositToGuildWarehouse() != null && item.getCanDepositToGuildWarehouse().equalsIgnoreCase("TRUE"))
			mask += (1 << 4);
		if (item.getBreakable() != null && item.getBreakable().equalsIgnoreCase("TRUE"))
			mask += (1 << 5);

		if (item.getSoulBind() != null && item.getSoulBind().equalsIgnoreCase("TRUE"))
			mask += (1 << 7);
		if (item.getNoEnchant() == null || item.getNoEnchant().equalsIgnoreCase("FALSE"))
			mask += (1 << 8);
		if (item.getCanDepositToAccountWarehouse().equalsIgnoreCase("TRUE"))
			mask += (1 << 9);
		if (item.getItemDropPermitted() != null && item.getItemDropPermitted().equalsIgnoreCase("TRUE"))
			mask += (1 << 10);
		if (item.getCanCompositeWeapon() != null && item.getCanCompositeWeapon().equalsIgnoreCase("TRUE"))
			mask += (1 << 11);
		if (item.getCashItem()>0)
			mask += (1 << 12);
		if (item.getCanSplit() != null && item.getCanSplit().equalsIgnoreCase("TRUE"))
			mask += (1 << 13);
		//if (item.getCannotChangeskin() > 0)
			mask += (1 << 14);
		
		//System.out.println(mask);
		return mask;
	}

	private static ArmorType getArmorType(String text)
	{
		if (text.equalsIgnoreCase("clothes"))
			return ArmorType.CLOTHES;
		else if (text.equalsIgnoreCase("no_armor"))
			{
			
			}
		else if (text.equalsIgnoreCase("plate"))
			return ArmorType.PLATE;
		else if (text.equalsIgnoreCase("chain"))
			return ArmorType.CHAIN;
		else if (text.equalsIgnoreCase("leather"))
			return ArmorType.LEATHER;
		else if (text.equalsIgnoreCase("robe"))
			return ArmorType.ROBE;
		else
			System.out.println("Missing armor type: " + text);
		return null;
	}

	private static WeaponType getWeaponType(String text)
	{

		if (text.equalsIgnoreCase("1h_dagger"))
			return WeaponType.DAGGER_1_H;
		if (text.equalsIgnoreCase("1h_mace"))
			return WeaponType.MACE_1_H;
		if (text.equalsIgnoreCase("1h_sword"))
			return WeaponType.SWORD_1_H;
		if (text.equalsIgnoreCase("1h_toolhoe"))
			return WeaponType.TOOLHOE_1_H;
		if (text.equalsIgnoreCase("2h_toolrod"))
			return WeaponType.TOOLROD_2_H;
		if (text.equalsIgnoreCase("2h_book"))
			return WeaponType.BOOK_2_H;
		if (text.equalsIgnoreCase("2h_orb"))
			return WeaponType.ORB_2_H;
		if (text.equalsIgnoreCase("2h_polearm"))
			return WeaponType.POLEARM_2_H;
		if (text.equalsIgnoreCase("2h_staff"))
			return WeaponType.STAFF_2_H;
		if (text.equalsIgnoreCase("2h_sword"))
			return WeaponType.SWORD_2_H;
		if (text.equalsIgnoreCase("2h_toolpick"))
			return WeaponType.TOOLPICK_2_H;
		if (text.equalsIgnoreCase("2h_toolrod"))
			return WeaponType.TOOLROD_2_H;
		if (text.equalsIgnoreCase("bow"))
			return WeaponType.BOW;
		System.out.println(text);
		return null;
	}

	public static List<Integer> getRestrict(ClientItem item)
	{
		List<Integer> restrict = new ArrayList<Integer>();
		restrict.add(item.getWarrior());
		restrict.add(item.getFighter());
		restrict.add(item.getKnight());
		restrict.add(item.getScout());
		restrict.add(item.getAssassin());
		restrict.add(item.getRanger());
		restrict.add(item.getMage());
		restrict.add(item.getWizard());
		restrict.add(item.getElementalist());
		restrict.add(item.getPriest());
		restrict.add(item.getCleric());
		restrict.add(item.getChanter());
		return restrict;
	}

	public static List<SimpleModifier> getBonusModifier(ClientItem item)
	{
		List<SimpleModifier> modifiers = new ArrayList<SimpleModifier>();
		convertBonus(item.getBonusAttr1(), modifiers);
		convertBonus(item.getBonusAttr2(), modifiers);
		convertBonus(item.getBonusAttr3(), modifiers);
		convertBonus(item.getBonusAttr4(), modifiers);
		convertBonus(item.getBonusAttr5(), modifiers);
		convertBonus(item.getBonusAttr6(), modifiers);
		convertBonus(item.getBonusAttr7(), modifiers);
		convertBonus(item.getBonusAttr8(), modifiers);
		convertBonus(item.getBonusAttr9(), modifiers);
		convertBonus(item.getBonusAttr10(), modifiers);
		convertBonus(item.getBonusAttr11(), modifiers);
		convertBonus(item.getBonusAttr12(), modifiers);
		return modifiers;
	}

	private static void convertBonus(String text, List<SimpleModifier> modifiers)
	{
		if (text == null)
			return;
		StringTokenizer st = new StringTokenizer(text, " ");
		if (st.countTokens() != 2)
			return;
		String stat = st.nextToken();
		String valueString = st.nextToken();
		boolean rate = false;
		if (valueString.substring(valueString.length() - 1).equalsIgnoreCase("%"))
		{
			rate = true;
			valueString = valueString.substring(0, valueString.length() - 1);
		}
		int value = Integer.valueOf(valueString);

		
		SimpleModifier modifier;
		if (rate)
			modifier = new RateModifier();
		else
			modifier = new AddModifier();
		modifier.setName(getModifiersEnum(stat));
		switch (modifier.getName())
		{
			case ATTACK_SPEED:
			case BOOST_CASTING_TIME:
				value = -value;
		}
		modifier.setBonus(true);
		modifier.setValue(value);
		modifiers.add(modifier);
	}

	private static Modifiersenum getModifiersEnum(String stat)
	{
		Modifiersenum modenum = null;

		if (stat.equalsIgnoreCase("phyAttack"))
			modenum = Modifiersenum.PHYSICAL_ATTACK;
		else if (stat.equalsIgnoreCase("dodge"))
			modenum = Modifiersenum.EVASION;
		else if (stat.equalsIgnoreCase("maxHp"))
			modenum = Modifiersenum.MAXHP;
		else if (stat.equalsIgnoreCase("maxMp"))
			modenum = Modifiersenum.MAXMP;
		else if (stat.equalsIgnoreCase("maxfp"))
			modenum = Modifiersenum.FLY_TIME;
		else if (stat.equalsIgnoreCase("parry"))
			modenum = Modifiersenum.PARRY;
		else if (stat.equalsIgnoreCase("Concentration"))
			modenum = Modifiersenum.CONCENTRATION;
		else if (stat.equalsIgnoreCase("MagicalResist"))
			modenum = Modifiersenum.MAGICAL_RESIST;
		else if (stat.equalsIgnoreCase("Magical_Resist"))
			modenum = Modifiersenum.MAGICAL_RESIST;
		else if (stat.equalsIgnoreCase("physicalcriticaldamagereduce"))
			modenum = Modifiersenum.PHYSICAL_CRITICAL_DAMAGE_REDUCE;
		else if (stat.equalsIgnoreCase("PhysicalCriticalReduceRate"))
			modenum = Modifiersenum.PHYSICAL_CRITICAL_RESIST;
		else if (stat.equalsIgnoreCase("critical"))
			modenum = Modifiersenum.PHYSICAL_CRITICAL;
		else if (stat.equalsIgnoreCase("PhysicalDefend"))
			modenum = Modifiersenum.PHYSICAL_DEFENSE;
		else if (stat.equalsIgnoreCase("magicalskillboost"))
			modenum = Modifiersenum.BOOST_MAGICAL_SKILL;
		else if (stat.equalsIgnoreCase("ElementalDefendFire"))
			modenum = Modifiersenum.FIRE_RESISTANCE;
		else if (stat.equalsIgnoreCase("ElementalDefendEarth"))
			modenum = Modifiersenum.EARTH_RESISTANCE;
		else if (stat.equalsIgnoreCase("ElementalDefendAir"))
			modenum = Modifiersenum.WIND_RESISTANCE;
		else if (stat.equalsIgnoreCase("ElementalDefendWater"))
			modenum = Modifiersenum.WIND_RESISTANCE;
		else if (stat.equalsIgnoreCase("flyspeed"))
			modenum = Modifiersenum.FLY_SPEED;
		else if (stat.equalsIgnoreCase("block"))
			modenum = Modifiersenum.BLOCK;
		else if (stat.equalsIgnoreCase("Pvpdefendratio"))
			modenum = Modifiersenum.PVP_DEFEND_RATIO;
		else if (stat.equalsIgnoreCase("Pvpattackratio"))
			modenum = Modifiersenum.PVP_ATTACK_RATIO;
		else if (stat.equalsIgnoreCase("hitAccuracy"))
			modenum = Modifiersenum.PHYSICAL_ACCURACY;
		else if (stat.equalsIgnoreCase("magicalhitAccuracy"))
			modenum = Modifiersenum.MAGICAL_ACCURACY;
		else if (stat.equalsIgnoreCase("magicalAttack"))
			modenum = Modifiersenum.MAGICAL_ATTACK;
		else if (stat.equalsIgnoreCase("BoostHate"))
			modenum = Modifiersenum.BOOST_HATE;
		else if (stat.equalsIgnoreCase("HealSkillBoost"))
			modenum = Modifiersenum.BOOST_HEAL;
		else if (stat.equalsIgnoreCase("speed"))
			modenum = Modifiersenum.SPEED;
		else if (stat.equalsIgnoreCase("magicalCritical"))
			modenum = Modifiersenum.MAGICAL_CRITICAL;
		else if (stat.equalsIgnoreCase("magicalCriticalReduceRate"))
			modenum = Modifiersenum.MAGICAL_CRITICAL_RESIST;
		else if (stat.equalsIgnoreCase("magicalCriticalDamageReduce"))
			modenum = Modifiersenum.MAGICAL_CRITICAL_DAMAGE_REDUCE;
		else if (stat.equalsIgnoreCase("attackDelay"))
			modenum = Modifiersenum.ATTACK_SPEED;
		else if (stat.equalsIgnoreCase("BoostCastingTime"))
			modenum = Modifiersenum.BOOST_CASTING_TIME;
		else if (stat.equalsIgnoreCase("arSpin"))
			modenum = Modifiersenum.SPIN_RESISTANCE;
		else if (stat.equalsIgnoreCase("arParalyze"))
			modenum = Modifiersenum.PARALYZE_RESISTANCE;
		else if (stat.equalsIgnoreCase("arSlow"))
			modenum = Modifiersenum.SLOW_RESISTANCE;
		else if (stat.equalsIgnoreCase("arSnare"))
			modenum = Modifiersenum.SNARE_RESISTANCE;
		else if (stat.equalsIgnoreCase("arOpenAreial"))
			modenum = Modifiersenum.OPENAREIAL_RESISTANCE;
		else if (stat.equalsIgnoreCase("arStagger"))
			modenum = Modifiersenum.STAGGER_RESISTANCE;
		else if (stat.equalsIgnoreCase("arStumble"))
			modenum = Modifiersenum.STUMBLE_RESISTANCE;
		else if (stat.equalsIgnoreCase("arPerification"))
			modenum = Modifiersenum.PERIFICATION_RESISTANCE;
		else if (stat.equalsIgnoreCase("arStun"))
			modenum = Modifiersenum.STUN_RESISTANCE;
		else if (stat.equalsIgnoreCase("arConfuse"))
			modenum = Modifiersenum.CONFUSE_RESISTANCE;
		else if (stat.equalsIgnoreCase("arCurse"))
			modenum = Modifiersenum.CURSE_RESISTANCE;
		else if (stat.equalsIgnoreCase("arFear"))
			modenum = Modifiersenum.FEAR_RESISTANCE;
		else if (stat.equalsIgnoreCase("arSilence"))
			modenum = Modifiersenum.SILENCE_RESISTANCE;
		else if (stat.equalsIgnoreCase("arPoison"))
			modenum = Modifiersenum.POISON_RESISTANCE;
		else if (stat.equalsIgnoreCase("arBleed"))
			modenum = Modifiersenum.BLEED_RESISTANCE;
		else if (stat.equalsIgnoreCase("arSleep"))
			modenum = Modifiersenum.SLEEP_RESISTANCE;
		else if (stat.equalsIgnoreCase("arRoot"))
			modenum = Modifiersenum.ROOT_RESISTANCE;
		else if (stat.equalsIgnoreCase("arBlind"))
			modenum = Modifiersenum.BLIND_RESISTANCE;
		else if (stat.equalsIgnoreCase("arCharm"))
			modenum = Modifiersenum.CHARM_RESISTANCE;
		else if (stat.equalsIgnoreCase("arDisease"))
			modenum = Modifiersenum.DISEASE_RESISTANCE;
		else
		{
			System.out.println("Missing Modifier:" + stat);
			return null;
		}
		return modenum;
	}

	private static int getItemSlot(String text)
	{
		int slot = 0;
		if (text.equalsIgnoreCase("main_or_sub"))
			return 3;
		else if (text.equalsIgnoreCase("right_or_left_battery"))
			return 24576;
		else if (text.equalsIgnoreCase("wing"))
			return 1 << 15;
		else if (text.equalsIgnoreCase("sub"))
			return 1 << 1;
		else if (text.equalsIgnoreCase("head"))
			return (1 << 2);
		else if (text.equalsIgnoreCase("waist"))
			return (1 << 16);
		else if (text.equalsIgnoreCase("neck"))
			return (1 << 10);
		else if (text.equalsIgnoreCase("right_or_left_ear"))
			return 192;
		else if (text.equalsIgnoreCase("main"))
			return 1;
		else if (text.equalsIgnoreCase("right_or_left_finger"))
			return 768;
		else if (text.equalsIgnoreCase("leg"))
			return 4096;
		else if (text.equalsIgnoreCase("shoulder"))
			return 2048;
		else if (text.equalsIgnoreCase("foot"))
			return 32;
		else if (text.equalsIgnoreCase("glove"))
			return (1 << 4);
		else if (text.equalsIgnoreCase("torso"))
			return (1 << 3);
		else if (text.equalsIgnoreCase("torso glove foot shoulder leg"))
			return 6200;
		else
			System.out.println("Missing slotId:" + text);

		return slot;
	}
}
